package com.jwm.power.filter;


import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebFilter(filterName = "FilterSession", urlPatterns = "/*")
public class FilterSession implements Filter {

    public static ThreadLocal<HttpServletRequest> threadLocalRequest = new ThreadLocal<HttpServletRequest>();
    public static ThreadLocal<HttpServletResponse> threadLocalResponse = new ThreadLocal<HttpServletResponse>();

    public FilterSession() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }

    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        // place your code here
        try {
            threadLocalRequest.set((HttpServletRequest) request);
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            HttpSession session = httpRequest.getSession(true);
            threadLocalResponse.set((HttpServletResponse) response);
            String[] notFilter = new String[]{"Login.jsp","Demo.jsp"};
            StringBuffer url = httpRequest.getRequestURL();
            if (url.indexOf("jsp") != -1) {
                boolean dofilter = true;
                for (String s : notFilter) {
                    if (url.indexOf(s) != -1) {
                        dofilter = false;
                        break;
                    }
                }
                if (dofilter) {
                    if (!StpUtil.isLogin()) {
                        response.setContentType("text/html;charset=UTF-8");
                        response.setCharacterEncoding("UTF-8");
                        PrintWriter printWriter = response.getWriter();
                        StringBuilder builder = new StringBuilder();
                        builder.append("<script>window.top.location.href=\"");
                        builder.append(httpRequest.getSession().getServletContext().getContextPath() + "/Login.jsp\";</script>");
                        printWriter.print(builder.toString());
                        printWriter.flush();
                        printWriter.close();
                    } else {
                        chain.doFilter(request, response);
                    }
                } else {
                    chain.doFilter(request, response);
                }
            } else {
                chain.doFilter(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
//            throw e;
        }
    }

    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) {
        // TODO Auto-generated method stub
    }

    public static boolean isAjaxRequest(HttpServletRequest request) {
        return request.getRequestURI().startsWith("/api");
        // String requestType = request.getHeader("X-Requested-With");
        // return requestType != null && requestType.equals("XMLHttpRequest");
    }
}
